/*****************************************************************************/
/** \file mc_hall.h
**
** 
**
** History: CCH
** 
*****************************************************************************/
#ifndef  HALL_H
#define  HALL_H


#include "type_define.h"



/*****************************************************************************/
/* Include files */
/*****************************************************************************/


/********OK 18000*********/
#define Hall_A  		GPIO1->DI_f.P3//GPIO2->DI_f.P3
#define Hall_B  		GPIO1->DI_f.P4//GPIO2->DI_f.P4
#define Hall_C  		GPIO1->DI_f.P2//GPIO2->DI_f.P2


/*****OK******/
//#define Hall_A  		GPIO1->DI_f.P3//GPIO2->DI_f.P3
//#define Hall_C  		GPIO1->DI_f.P4//GPIO2->DI_f.P4
//#define Hall_B  		GPIO1->DI_f.P2//GPIO2->DI_f.P2



//#define Hall_B  		GPIO1->DI_f.P3//GPIO2->DI_f.P3
//#define Hall_A  		GPIO1->DI_f.P4//GPIO2->DI_f.P4
//#define Hall_C  		GPIO1->DI_f.P2//GPIO2->DI_f.P2



//#define Hall_B  		GPIO1->DI_f.P3//GPIO2->DI_f.P3
//#define Hall_C  		GPIO1->DI_f.P4//GPIO2->DI_f.P4
//#define Hall_A  		GPIO1->DI_f.P2//GPIO2->DI_f.P2


/**********OK -2000**********/
//#define Hall_C  		GPIO1->DI_f.P3//GPIO2->DI_f.P3
//#define Hall_A  		GPIO1->DI_f.P4//GPIO2->DI_f.P4
//#define Hall_B  		GPIO1->DI_f.P2//GPIO2->DI_f.P2


//#define Hall_C  		GPIO1->DI_f.P3//GPIO2->DI_f.P3
//#define Hall_B  		GPIO1->DI_f.P4//GPIO2->DI_f.P4
//#define Hall_A  		GPIO1->DI_f.P2//GPIO2->DI_f.P2


/*****************************************************************************/
/* Global pre-processor symbols/macros ('#define') */
/*****************************************************************************/
#define DEG_360 	(65535)
#define DEG_330 	(60074)
#define DEG_300 	(54613)
#define DEG_270 	(49152)
#define DEG_240 	(43690)
#define DEG_210 	(38229)
#define DEG_180 	(32767)
#define DEG_150 	(27306)
#define DEG_120 	(21845)
#define DEG_90  	(16384)
#define DEG_60  	(10923)
#define DEG_30  	(5461)
#define DEG_0  	 	(0)



#define CW_DIR											1
#define CCW_DIR											0


#define	SquareIntCNT								60			
		


/*****************************************************************************/
/* Global type definitions ('typedef') */
/*****************************************************************************/
typedef struct 
{	
	u8	FGCnt;
	u8  Value;
	u8  NowValue;
	u8  NextValue;
	u8  LastValue;
	u8 	DirFlag;
	u8	HallIntCnt;
	u8	WindmillHallIntCnt;
	u8	CWCnt;
	u8	CCWCnt;
	u8	AngleSector;
	s16	Angle;
	s16	HallAngle;

	s16	DeltaTheta;
	s16	StepTheta;
	s16 StepThetaVal;
	
	
	s32	HallOmegaPeroid;
	s32 HallOmegaPeroid2;
	s32	HallOmegaL1;
	s32	HallOmegaH1;
	s32	Erpm;	
	
	u32 TimCount[6];
	u32 TimCount1[6];
	
	
	s16	HallACnt;
	s16	HallBCnt;
	s16	HallCCnt;
	
//	s16	HallAGNDCnt;
//	s16	HallAVCCCnt;
//	
//	s16	HallBGNDCnt;
//	s16	HallBVCCCnt;

//	s16	HallCGNDCnt;
//	s16	HallCVCCCnt;
	
	s16	HallBrokenCnt;
	
	
	s16 TargetWKAngle;
//	s16 CCWHeadAngle;
}HALL_T;




/*****************************************************************************/
/* Global variable declarations ('extern', definition in C source) */
/*****************************************************************************/
extern volatile HALL_T	Hall;
extern const u8 CWHALLSectorValue[6];//hall 1 = 30C,0; 3 = 90C,1; 2 = 150C,2; 6 = 210C,3; 4 = 270C,4; 5 = 330C,5;
extern const u8 CCWHALLSectorValue[6];//210C,150C,90C,30C,330C,270C;
extern const u8 HALL_ORDER_CW_UBROKEN[4];
extern const u8 HALL_ORDER_CW_VBROKEN[4];
extern const u8 HALL_ORDER_CW_WBROKEN[4];




extern const u32 HALL_Angle_Val[6];
extern const u32 HALL_Angle_CW_UBROKEN[4];
extern const u32 HALL_Angle_CW_VBROKEN[4]; 
extern const u32 HALL_Angle_CW_WBROKEN[4]; 
/*****************************************************************************/
/* Global function prototypes ('extern', definition in C source) */
/*****************************************************************************/
void HALLGpioInit(void);
void MotorHallRunFun(void);
void HallNextValScanCWFun(void);
void HallLastValScanCWFun(void);
void HallNextValScanCCWFun(void);
void HallLastValScanCCWFun(void);
void GetHallValIdleFun(void);
void FocHallAngleAdjustFun(void);
void HallValInitFun(void);
void NormalHallRunFun(void);
void UnnormalHallRunFun(void);
void NormalHallOmegaCalFun(void);
void GetBrokenHallStateStartFun(void);
void GetSpeedTim0ValFun(void);
void WeakenFieldControlFun(void);
void HallCntResetFun(void);
void HallValResetFun(void);


#endif	/* __MC_HALL_H__ */



